package com.sign2024.sign2024project.goodsmaterials.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sign2024.sign2024core.common.CommonResult;
import com.sign2024.sign2024core.common.annotation.Log;
import com.sign2024.sign2024core.common.constants.CommonConstants;
import com.sign2024.sign2024core.common.exeception.CommonException;
import com.sign2024.sign2024core.common.param.PageParam;
import com.sign2024.sign2024core.entry.goodsmaterials.WzHistoryData;
import com.sign2024.sign2024project.goodsmaterials.controller.test.NoModelDataListener;
import com.sign2024.sign2024project.goodsmaterials.service.WzHistoryDataService;
import com.sign2024.sign2024project.goodsmaterials.vo.WzHistoryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * 物资历史抽取方案
 *
 * @author yujie
 * @data 2024/8/27 13:55
 */
@Api(tags = "物资")
@RestController
@RequestMapping("/api/wzHistoryData")
public class WzHistoryDataController {

    @Resource
    private WzHistoryDataService wzHistoryDataService;

    // 测试不创建对象读
    @Transactional
    @PostMapping("/import")
    public CommonResult<WzHistoryData> importInfo(MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, new NoModelDataListener()).sheet().doRead();
            return CommonResult.success();
        } catch (IOException e) {
            e.printStackTrace();
            throw new CommonException(CommonConstants.ERROR, "excel数据导入失败");
        }
    }


//    //    @Log
//    @Transactional
////    @ApiOperation(value = "物资历史excel导入", notes = "物资历史excel导入")
//    @PostMapping("/import")
//    public CommonResult<WzHistoryData> importInfo(MultipartFile file) {
//        try {
//            InputStream inputStream = file.getInputStream();
////            List<WzHistoryData> list =
////                    EasyExcel.read(inputStream).head(WzHistoryData.class).sheet().doReadSync();
////            List<WzHistoryData> dataList = new ArrayList<>();
////            EasyExcel.read(inputStream, new CustomExcelListener(dataList)).sheet().doRead();
//
//            EasyExcel.read(inputStream, new NoModelDataListener()).sheet().doRead();
//
////            dataList.forEach(System.out::println);
////            System.out.println("测试导入");
//
////            wzHistoryDataService.exportData(list);
//            return CommonResult.success();
//        } catch (IOException e) {
//            e.printStackTrace();
//            throw new CommonException(CommonConstants.ERROR, "excel数据导入失败");
//        }
//    }

    @Log(title = "物资", businessType = "查询")
    @ApiOperation(value = "分页查询物资历史数据", notes = "分页查询物资历史数据")
    @PostMapping("/page")
    public CommonResult<WzHistoryData> page(PageParam param) {
        Page<WzHistoryData> page = wzHistoryDataService.pageWzHistoryData(param);
        return CommonResult.success(page);
    }

    @Log(title = "物资", businessType = "查询")
    @ApiOperation(value = "查询标段历史频次", notes = "查询标段历史频次")
    @PostMapping("/select")
    public CommonResult<WzHistoryVo> selectVo(@RequestParam @ApiParam("标段名称") String sectionName,
                                              @RequestParam @ApiParam("频次") Integer skillNum) {
        List<WzHistoryVo> page = wzHistoryDataService.selectHistoryVo(sectionName, skillNum);
        return CommonResult.success(page);
    }

    @Log
    @Transactional
    @ApiOperation(value = "物资历史抽取方案excel导入", notes = "物资历史抽取方案excel导入")
    @PostMapping("/importWzHistory")
    public CommonResult<WzHistoryData> importProject(MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            List<WzHistoryData> list =
                    EasyExcel.read(inputStream).head(WzHistoryData.class).sheet().doReadSync();
            wzHistoryDataService.importWzHistory(list);
            return CommonResult.success();
        } catch (IOException e) {
            e.printStackTrace();
            throw new CommonException(CommonConstants.ERROR, "excel数据导入失败");
        }
    }
}
